package com.qj.yantaishan.common.exception;

import com.qj.yantaishan.common.core.domain.R;

import lombok.SneakyThrows;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class ResponseException {
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public static  R methodError(HttpServletResponse response,Exception e){
        e.printStackTrace();
        response.setStatus(405);
        return R.fail(405,"请求方法错误");
    }
    @SneakyThrows
    @ExceptionHandler(UnLogin.class)
    public static R unLogin(HttpServletResponse response,Exception e) {
        response.setStatus(401);
        e.printStackTrace();
        return R.fail("未登录,请先登录!");
    }

    @ExceptionHandler({ParamsException.class, MissingServletRequestParameterException.class})
    public static R params(HttpServletResponse response,Exception e) {
        e.printStackTrace();
        response.setStatus(400);
        return R.fail(400,"参数错误");
    }

    @ExceptionHandler(Exception.class)
    public static  R error(HttpServletResponse response,Exception e){
        e.printStackTrace();
        response.setStatus(500);
        return R.fail("系统异常");
    }

}

